package cn.ipenta.jkb.web;

import java.util.List;
import java.util.Map;
import java.util.Random;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSON;

import cn.ipenta.jkb.JkbWebException;
import cn.ipenta.jkb.entity.ChildInfo;
import cn.ipenta.jkb.entity.Healer;
import cn.ipenta.jkb.entity.Regulator;
import cn.ipenta.jkb.entity.TxInfo;
import cn.ipenta.jkb.service.CustomerService;
import cn.ipenta.jkb.service.RegulatorService;
import cn.ipenta.jkb.utils.JsonMapper;
import cn.ipenta.jkb.web.vo.ApplyVo;
import cn.ipenta.jkb.web.vo.PayVo;
import net.sf.cglib.beans.BeanCopier;

/**
 * @ClassName: CustomerController
 * @Description: 客户控制类，用来面向用户供应
 * @author ipenta
 * @date 2017年4月7日 上午10:53:51
 * 
 */
@Controller
public class CustomerController {

	private static Random random = new Random();

	final BeanCopier copier = BeanCopier.create(PayVo.class, TxInfo.class, false);

	final Logger logger = LoggerFactory.getLogger(CustomerController.class);

	@Autowired
	CustomerService customerService;

	@Autowired
	RegulatorService regulatorService;

	@GetMapping("/api/regulators")
	public @ResponseBody List<Regulator> regulators() {
		return customerService.findRegulatorsAll();
	}

	@GetMapping("/api/healers")
	public @ResponseBody List<Healer> healers() {
		return customerService.findHealersAll();
	}

	@PostMapping("/api/customer/apply")
	public @ResponseBody Map<String, Object> registerAndApply(@RequestBody ApplyVo applyVo)
			throws JkbWebException {

		String regulatorid = "00000000000000000";
		logger.debug("722---{}", JSON.toJSONString(applyVo));

		ChildInfo childInfo = new ChildInfo(applyVo.getChildname(), applyVo.getChildgender(),
				applyVo.getChildbirthday());

		// TODO 随机注入监管机构ID,测试用,待删除
		List<Regulator> regulators = regulatorService.getRegulatorlist();
		if (!regulators.isEmpty()) {
			regulatorid = regulators.get(random.nextInt(regulators.size())).getId();
		}

		TxInfo txInfo = new TxInfo(regulatorid, applyVo.getCustomerid(), applyVo.getType(), applyVo.getNum());

		Boolean isApply = customerService.checkChildAndApply(childInfo, txInfo);

		return JsonMapper.success(isApply);
	}

	@PostMapping("/api/customer/pay")
	public @ResponseBody Map<String, Object> pay(@RequestBody PayVo payVo) throws JkbWebException {
		TxInfo txInfo = new TxInfo();
		copier.copy(payVo, txInfo, null);
		Boolean isPaid = customerService.createPay(txInfo);

		return JsonMapper.success(isPaid);
	}

	@GetMapping("/api/customer/apply/list")
	public @ResponseBody Map<String, Object> findApplyList(@RequestParam String id) throws JkbWebException {
		List<TxInfo> txInfos = customerService.findApplyRecordById(id);
		return JsonMapper.success(txInfos);
	}

	@GetMapping("/api/customer/pay/list")
	public @ResponseBody Map<String, Object> findPayList(@RequestParam String id) throws JkbWebException {
		List<TxInfo> txInfos = customerService.findPayRecordById(id);
		return JsonMapper.success(txInfos);
	}

	@GetMapping("/api/customer/tx/list")
	public @ResponseBody Map<String, Object> findTxList(@RequestParam String id,
			@RequestParam(required = false) String order, @RequestParam(required = false) String sortBy)
			throws JkbWebException {

		List<TxInfo> txInfos = customerService.findTxRecordByIdAndOrderAndSort(id, order, sortBy);
		return JsonMapper.success(txInfos);
	}

}
